package com.yonyou.pmclouds.billview.rmiimpl.buzi;

import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.basecom.util.DateUtils;
import com.yonyou.pmclouds.billview.entity.BillViewQueryVO;
import com.yonyou.pmclouds.billview.rmitf.IBillViewBuziService;
import com.yonyou.pmclouds.billview.util.BillViewUtil;
import com.yonyou.pmclouds.problemlog.entity.ProblemLogBillStatus;
import com.yonyou.pmclouds.problemlog.entity.ProblemLogVO;
import com.yonyou.pmclouds.problemlog.service.rmiitf.ProblemLogQuery;
import com.yonyou.pmclouds.team.entity.TeamMemberVO;
import com.yonyou.pmclouds.team.rmiitf.TeamMemberQuery;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ProblemLogBuziServiceImp implements IBillViewBuziService {

    @Autowired
    private ProblemLogQuery problemLogQuery;
    @Autowired
    private TeamMemberQuery teamMemberQuery;

    @Override
    public String getBillType() {
        return BillTypeConst.PROBLEMLOG_BILLTYPE;
    }

    @Override
    public PageObject<Object> queryBillList(BillViewQueryVO billViewQueryVO) throws BusinessException {

        PageObject<Object> pageProblem = problemLogQuery.queryByCondition(billViewQueryVO.getPkProject(), billViewQueryVO.getCreators(), billViewQueryVO.getStartTime(),
                billViewQueryVO.getEndTime(), billViewQueryVO.getBillStatus(), billViewQueryVO.getPageStart(), billViewQueryVO.getPageSize());

        ProblemLogVO[] problemLogVOs = (ProblemLogVO[]) pageProblem.getObjects();

        if (ArrayUtils.isEmpty(problemLogVOs))
            return pageProblem;

        //设置超期标识
        for (ProblemLogVO problemLogVO : problemLogVOs) {
            if (problemLogVO.getEnddate() == null || problemLogVO.getBillstatus() == ProblemLogBillStatus.STATUS_END)
                continue;

            if (DateUtils.asDateEnd(problemLogVO.getEnddate()).before(DateUtils.asDateEnd(new Date()))) {
                problemLogVO.setOverTime(true);
            }
        }

        setBillStatus(problemLogVOs);
        pageProblem.setObjects(problemLogVOs);

        return pageProblem;
    }

    @Override
    public SuperVO queryBillDetail(String pkBill) throws BusinessException {

        ProblemLogVO problemLogVO = problemLogQuery.queryOne(pkBill);

        TeamMemberVO[] teamMemberVOs = teamMemberQuery.queryByProAndPkUsers(problemLogVO.getPk_project(), new String[]{problemLogVO.getPk_rectificationer()});

        if (ArrayUtils.isNotEmpty(teamMemberVOs) && teamMemberVOs[0].getPkEnterprise() != null)
            problemLogVO.setPkRectifyEnter(teamMemberVOs[0].getPkEnterprise());
        setBillStatus(new ProblemLogVO[]{problemLogVO});


        if (problemLogVO.getEnddate() != null) {
            BillViewUtil.setFormatDate(new ProblemLogVO[]{problemLogVO}, new String[]{ProblemLogVO.END_DATE}, new String[]{"strenddate"});
        }
        BillViewUtil.setFormatDate(new ProblemLogVO[]{problemLogVO}, new String[]{CommonFieldConst.CREATIONTIME}, new String[]{"strCreationtime"});

        return problemLogVO;
    }

    private void setBillStatus(ProblemLogVO[] problemLogVOs) {

        for (ProblemLogVO problemLogVO :
                problemLogVOs) {
            byte billStatus = problemLogVO.getBillstatus();
            String strBillStatus = "";
            switch (billStatus) {
                case ProblemLogBillStatus.STATUS_REPAIRING:
                    strBillStatus = "待整改";
                    break;
                case ProblemLogBillStatus.STATUS_ACCEPTING:
                    strBillStatus = "待接收";
                    break;
                case ProblemLogBillStatus.STATUS_END:
                    strBillStatus = "已关闭";
                    break;
                case ProblemLogBillStatus.STATUS_PASS:
                    strBillStatus = "通过";
                    break;
                case ProblemLogBillStatus.STATUS_NOT_PASS:
                    strBillStatus = "不通过";
                    break;
                default:
                    strBillStatus = "";
            }

            problemLogVO.setStrBillstatus(strBillStatus);
        }
    }
}
